package com.wdc.demo_netty_1_05nettyAdapter.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioChannelOption;
import io.netty.channel.socket.nio.NioSocketChannel;

public class NettyClient {
    public static void main(String[] args) {
        new NettyClient().connect("127.0.0.1",7397);
    }

    private void connect(String inetHost, int inetPort) {
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            Bootstrap b = new Bootstrap()
                    .group(workerGroup)
                    .channel(NioSocketChannel.class)
                    .option(NioChannelOption.AUTO_READ,true)
                    .handler(new MyChannelInitializer());
            ChannelFuture f = b.connect(inetHost,inetPort).sync();
            System.out.println("Client start done");
            f.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            workerGroup.shutdownGracefully();
        }

    }

}
